import { Tween, Easing } from '@tweenjs/tween.js';
import { OrbitControls } from 'three/examples/jsm/Addons.js';
import * as THREE from 'three';

export default class {
  end: any;
  start: any;
  tween: any;
  control: OrbitControls;
  moveing: boolean = false;
  camera: THREE.PerspectiveCamera;
  constructor(camera: THREE.PerspectiveCamera, control: OrbitControls) {
    this.camera = camera;
    this.control = control;
  }
  move(data: any, onStart = () => void 0, onComplete = () => void 0) {
    if (this.moveing) return;
    this.start = this.getStatPoint();
    this.end = this.getEndPoint(data);
    this.tween = new Tween(this.start).to(this.end, 1500);
    this.tween.easing(Easing.Quadratic.InOut);

    this.tween.onUpdate((e: any) => {
      const [x, y, z, a, b, c] = e;
      this.control.target.set(a, b, c);
      this.camera.position.set(x, y, z);
    });

    this.tween.onStart(() => {
      this.control.enabled = false;
      onStart && onStart();
    });

    this.tween.onComplete(() => {
      this.moveing = false;
      this.control.enabled = true;
      onComplete && onComplete();
    });

    this.tween.start();
    this.moveing = true;
  }
  update(time: number) {
    if (this.moveing) this.tween.update(time);
  }
  getStatPoint() {
    const target = this.control.target;
    const { x, y, z } = this.camera.position;
    return [x, y, z, target.x, target.y, target.z];
  }
  getEndPoint({
    camera,
    target
  }: {
    camera: number[] | THREE.Vector3;
    target: number[] | THREE.Vector3;
  }) {
    const result: any = [];
    if (Array.isArray(camera)) {
      result.push(...camera);
    } else {
      result.push(camera.x, camera.y, camera.z);
    }
    if (Array.isArray(target)) {
      result.push(...target);
    } else {
      result.push(target.x, target.y, target.z);
    }
    return result;
  }
}
